<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>注册</span>
        </div>
        <div class="text item" >
        <el-form
          :model="ruleForm" 
          status-icon
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名">
            <el-input type="text" v-model="ruleForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              type="password"
              v-model="ruleForm.userPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="上传头像">
            <el-upload
              class="avatar-uploader"
              action="/api/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="ruleForm.imgUrl" :src="ruleForm.imgUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
        </div>
      
    </el-card>
  </div>
</template>

<script>
import {register} from '../service/user'
export default {
  data() {
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
        imgUrl:"",
        userHeader:""
      },
    };
  },
  methods: {
    handleAvatarSuccess(data){
      console.log("data:",data)
      this.ruleForm.imgUrl = "/api/upload/" + data.data;
      this.ruleForm.userHeader = data.data;
    },
    async submitForm(formName) {
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     alert("submit!");
      //   } else {
      //     console.log("error submit!!");
      //     return false;
      //   }
      // });
      await register(this.ruleForm.userName
      ,this.ruleForm.userPass
      ,this.ruleForm.userHeader);
      alert("submit!");
      
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }
.box-card {
    margin: auto;
    width: 480px;
  }
</style>